package cn.tedu.em.advise.webapi.controller;

import cn.tedu.em.advise.service.IAdviseService;
import cn.tedu.em.common.pojo.advise.dto.AdviseAddNewDTO;
import cn.tedu.em.common.pojo.advise.dto.AdviseUpdateDTO;
import cn.tedu.em.common.pojo.advise.vo.AdviseListItemVO;
import cn.tedu.em.common.pojo.advise.vo.AdviseStandardVO;
import cn.tedu.em.common.restful.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/advises")
@Api(tags = "建议管理模块")
public class AdviseController {


    @Autowired
    private IAdviseService adviseService;

    // http://localhost:30007/advises/add-new
    @ApiOperation("添加建议")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(AdviseAddNewDTO adviseAddNewDTO){
        log.info("开始处理【添加建议】的请求，参数：{}", adviseAddNewDTO);
        adviseService.addNew(adviseAddNewDTO);
        String message = "提交建议成功!";
        return JsonResult.ok(message);
    }

    // http://localhost:30007/advises/9527/delete
    @ApiOperation("根据ID删除建议")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        log.debug("开始处理【根据ID删除建议】的请求，参数：{}", id);
        adviseService.delete(id);
        return JsonResult.ok("删除成功!");
    }

    // http://localhost:30007/advises/9527/update
    @ApiOperation("修改建议详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id, AdviseUpdateDTO adviseUpdateDTO){
        log.debug("开始处理【修改建议详情】的请求，参数ID：{}, 新数据：{}", id, adviseUpdateDTO);
        adviseService.updateInfoById(id, adviseUpdateDTO);
        return JsonResult.ok("修改成功!");
    }

    // http://localhost:30007/advises/9527
    @ApiOperation("根据ID查询建议详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AdviseStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据ID查询建议详情】的请求，参数：{}", id);
        AdviseStandardVO advise = adviseService.getStandardById(id);
        return JsonResult.ok(advise);

    }

    // http://localhost:30007/advises
    @ApiOperation("查询建议列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AdviseListItemVO>> list(){
        log.debug("开始处理【查询建议列表】的请求，无参数");
        List<AdviseListItemVO> list = adviseService.list();
        return JsonResult.ok(list);
    }
}
